package day1.HomeWork;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


/**
 * ClassName：Test3Client
 *
 * @Author：小鱼
 * @Create：2025-08-25 15:26
 */
public class Test3Client {
    /*
    ①从键盘录入老师信息，录入格式：姓名-年龄-工资，遇quit结束录入
      zs-31-8900
      tom-28-7893.5
      ww-33-4536.5
      ...
      quit
      ②将每行信息封装成老师对象，全部发送到服务器
      ③发送成功后，收取从服务器返回的信息并输出
      ④关闭客户端
    */
    public static void main(String[] args)throws Exception {

        //从键盘录入老师信息
        System.out.println("请输入老师信息，录入格式：姓名-年龄-工资，输入quit结束录入：");
        Scanner sc = new Scanner(System.in);
        List<Teacher> teachers = new ArrayList<Teacher>();
        while (true){
            String s = sc.nextLine();
            // 遇到quit结束录入
            if ("quit".equalsIgnoreCase(s)) {
                System.out.println("已结束录入");
                break;
            }
            //拆解上述整行的字符串得到属性值
            String[] result = s.split("-");
            Teacher teacher = new Teacher(result[0],Integer.parseInt(result[1]),Double.parseDouble(result[2]));
            teachers.add(teacher);
            //System.out.println("已添加老师信息: " + teacher);
        }

        //连接服务器
        String ip = "127.0.0.1";
        int port = 8002;
        Socket socket = new Socket(ip, port);
        System.out.println("成功连接服务器");
        //获取IO流，传输数据
        ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
        //发数据给服务器
        os.writeObject(teachers);
        os.flush();
        socket.shutdownOutput();
        System.out.println("客户端： 成功发送消息");

        //收取消息
        String response = (String) is.readObject();
        System.out.println("接收到服务器的消息为：" + response);

        //关闭客户端
        os.close();
        is.close();
        socket.close();
        sc.close();

    }
}
